import screenfull from 'screenfull'

export default {
  namespaced: true,
  state: {
    // 全屏激活
    isFullscreen: false
  },
  getters: {},
  actions: {
    listen({ commit }) {
      return new Promise(resolve => {
        if (screenfull.enabled) {
          screenfull.on('change', () => {
            // commit('SWITCH', screenfull.isFullscreen)
            if (!screenfull.isFullscreen) {
              commit('SWITCH', false)
            }
          })
        }
        // end
        resolve()
      })
    },
    toggle({ commit }) {
      return new Promise(resolve => {
        if (screenfull.isFullscreen) {
          screenfull.exit()
          commit('SWITCH', false)
        } else {
          screenfull.request()
          commit('SWITCH', true)
        }
        // end
        resolve()
      })
    },
    set({ commit }, isFullscreen) {
      commit('SWITCH', isFullscreen)
    },
    upate({ commit }) {
      console.log('update:', screenfull.isFullscreen)
      commit('SWITCH', screenfull.isFullscreen)
    }
  },
  mutations: {
    SWITCH(state, isFullscreen) {
      state.isFullscreen = isFullscreen
      console.log('切换全屏状态：', isFullscreen)
    }
  }
}
